package com.pktechan.api.service.impl;

import com.pktechan.api.base.BaseMapper;
import com.pktechan.api.base.BaseServiceImpl;
import com.pktechan.api.dao.CommissionWithdrawFlowMapper;
import com.pktechan.api.entity.CommissionBalance;
import com.pktechan.api.entity.CommissionWithdrawFlow;
import com.pktechan.api.enumeration.CommissionWithdrawStatusEnum;
import com.pktechan.api.sendMiniMessage.BaseSendMiniMessage;
import com.pktechan.api.sendMiniMessage.SendMiniMessageStrategy;
import com.pktechan.api.sendMiniMessage.bean.FactoryReferrerCommissionApplyMessage;
import com.pktechan.api.sendMiniMessage.enumration.SendMiniMessageEnum;
import com.pktechan.api.service.CommissionBalanceService;
import com.pktechan.api.service.CommissionWithdrawFlowService;
import com.pktechan.api.service.SendMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("commissionWithdrawFlowService")
@Transactional
public class CommissionWithdrawFlowServiceImpl extends BaseServiceImpl<CommissionWithdrawFlow> implements CommissionWithdrawFlowService {
    private static final Logger logger = LoggerFactory.getLogger(CommissionWithdrawFlowServiceImpl.class);

    @Autowired
    private CommissionWithdrawFlowMapper commissionWithdrawFlowMapper;
    @Autowired
    private CommissionBalanceService commissionBalanceService;
    @Autowired
    private SendMessageService sendMessageService;

    /**
     * 由业务类实现
     *
     * @return
     */
    @Override
    public BaseMapper<CommissionWithdrawFlow> getBaseMapper() {
        return commissionWithdrawFlowMapper;
    }

    /**
     * 根据custId查询对应佣金支付明细列表
     *
     * @param custId
     * @return
     */
    @Override
    public List<CommissionWithdrawFlow> getCommissionWithdrawDetailsListByCustId(Integer custId) {
        return commissionWithdrawFlowMapper.getCommissionWithdrawDetailsListByCustId(custId);
    }

    /**
     * 佣金提现
     *
     * @param commissionWithdrawFlow
     */
    @Override
    public void commissionWithdraw(CommissionWithdrawFlow commissionWithdrawFlow) {
        // 查询佣金信息
        logger.info("----------查询佣金信息id=" + commissionWithdrawFlow.getCommissionBalanceId() + "------------");
        CommissionBalance getById = commissionBalanceService.getById(commissionWithdrawFlow.getCommissionBalanceId());
        //添加流水信息
        commissionWithdrawFlow.setCustId(getById.getCustId());//客户ID
        commissionWithdrawFlow.setBalancePre(getById.getAvailableBalance());//提现前佣金
        commissionWithdrawFlow.setBalanceAfter(getById.getAvailableBalance().subtract(commissionWithdrawFlow.getWithdrawAmount()));//提现前佣金-申请提现金额=提现后佣金
        commissionWithdrawFlow.setStatus(CommissionWithdrawStatusEnum.WITHDRAW_TO_BE_COMPLETED.getCode());//提现待完成
        logger.info("添加佣金提现流水信息");
        commissionWithdrawFlowMapper.save(commissionWithdrawFlow);
        //修改佣金信息
        CommissionBalance commissionBalance = new CommissionBalance();
        commissionBalance.setAvailableBalance(getById.getAvailableBalance().subtract(commissionWithdrawFlow.getWithdrawAmount()));//可用余额-申请提现金额
        commissionBalance.setWithdrawFreezeAmount(getById.getWithdrawFreezeAmount().add(commissionWithdrawFlow.getWithdrawAmount()));//提现冻结金额+申请提现金额
        commissionBalance.setId(commissionWithdrawFlow.getCommissionBalanceId());
        logger.info("修改佣金信息");
        commissionBalanceService.update(commissionBalance);
        //厂家接收-推荐人-佣金提现-提现申请通知
        FactoryReferrerCommissionApplyMessage factoryReferrerCommissionApplyMessage = sendMessageService.getFactoryReferrerCommissionApplyMessage(commissionWithdrawFlow.getId());
        SendMiniMessageStrategy factoryReferrerCommissionApplyStrategy = BaseSendMiniMessage.getSendMiniMessage(SendMiniMessageEnum.FACTORY_REFERRER_COMMISSION_APPLY);
        factoryReferrerCommissionApplyStrategy.sendMiniMessageToActiveMq(factoryReferrerCommissionApplyMessage);
    }
}
